import os, sys
import pygame
from time import sleep
#from pygame.locals import *

if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'

class BaseJasGame():
    def __init__(self, caption, helptext="Press 'Esc' to exit"):
        #prepare pygame, fullscreen and white bg
        pygame.init()
        self.screen = pygame.display.set_mode((1024, 768), pygame.FULLSCREEN)
        pygame.display.set_caption(caption)
        pygame.mouse.set_visible(0)
        
        self.background = pygame.Surface(self.screen.get_size())
        self.background = self.background.convert()
        self.background.fill((200, 200, 250))
        self.caption_surface = None
        self.write_helptext(helptext)
    def quit(self):
        sys.exit()
    def write_helptext(self, helptext):
        #small text at bottom for big person to read.
        if pygame.font:
            font = pygame.font.Font(None, 14)
            helptext_surface = font.render(helptext, 1, (10, 10, 10), (250,250,250))
            textpos = helptext_surface.get_rect(center=(self.background.get_width()/2, self.background.get_height()*99/100))
            self.background.blit(helptext_surface, textpos)
    
    def write_caption(self, text):
        #caption at top of screen, for small person to read
        if pygame.font:
            font = pygame.font.Font(None, 52)
            if self.caption_surface != None: #blit over old caption
                x = pygame.Surface((self.caption_surface.get_size()))
                x.fill((250,250,250))
                textpos = self.caption_surface.get_rect(centerx=self.background.get_width()/2)
                self.background.blit(x, textpos)
            self.caption_surface = font.render(text, 1, (10, 10, 10), (250,250,250))
            textpos = self.caption_surface.get_rect(centerx=self.background.get_width()/2)
            self.background.blit(self.caption_surface, textpos)

    def bye(self, text='bye bye'):
        self.screen = pygame.display.set_mode((800, 600))
        #say goodbye, wait and exit
        if pygame.font:
            font = pygame.font.Font(None, 100)
            caption_surface = font.render(text, 1, (10, 10, 10))
            textpos = caption_surface.get_rect(center=(400, 300))
            self.background.blit(caption_surface, textpos)
            self.redraw()
        sleep(1)
        sys.exit()
        
    def redraw(self):
        #redraw sprites and update display
        self.allsprites.update()
        self.screen.blit(self.background, (0, 0))
        self.allsprites.draw(self.screen)
        pygame.display.flip()
